package com.itheima.health.tupian;

import com.itheima.health.common.RedisConst;
import com.itheima.health.util.QiniuUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Set;

@Component
@Slf4j
public class QiLiTuPian {
    @Autowired
    private QiniuUtils qiniuUtils;
    @Autowired
    private RedisTemplate redisTemplate;

    @Scheduled(cron = "0 0 2 * * ?")//每天凌晨俩点清理
    public void clearLmg() {
        log.info("清理图片");
        //查询redis开始清理图片
        Set<String> members = redisTemplate.opsForSet().members(RedisConst.SETMEAL_PIC_RESOURCES);
        for (String menmder : members) {
            //获取图片储存的时间
            String[] c = menmder.split("_");
            String time = c[0];
            //获取当前时间
            Long k = System.currentTimeMillis();
            Long inLong = Long.parseLong(time);
            if ((k - inLong) > 1800000L) {
                redisTemplate.opsForSet().remove(RedisConst.SETMEAL_PIC_RESOURCES, menmder);
                qiniuUtils.deleteFileFromQiniu(menmder);

            }
        }
    }
}
